package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.converter.VocChapterConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocChapterDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocChapterMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteChapterOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocChapterDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocChapterCommand;
import com.cskaoyan.wordmemorize.request.VocChapterPageRequest;
import com.cskaoyan.wordmemorize.service.VocChapterService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class VocChapterServiceImpl implements VocChapterService {

    @Resource
    private VocChapterMapper vocChapterMapper;
    @Resource
    private VocChapterConverter vocChapterConverter;

    @Override
    public PageDTO<VocChapterDTO> getChapterPage(VocChapterPageRequest pageQuery) {
        //条件构造器
        LambdaQueryWrapper<VocChapterDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(VocChapterDO::getBookId, pageQuery.getBookId());
        //判断是否分页
        if (pageQuery.getPageNum() == null || pageQuery.getCount() == null) {
            //进行全表查询
            List<VocChapterDO> vocChapterDOS = vocChapterMapper.selectList(queryWrapper);
            return vocChapterConverter.vocChapterDOs2Page(vocChapterDOS, null);
        }else{
            Page<VocChapterDO> page = new Page<>(pageQuery.getPageNum(), pageQuery.getCount());
            Page<VocChapterDO> result =  vocChapterMapper.selectPage(page, queryWrapper);
            PageDTO<VocChapterDTO> pageDTO = vocChapterConverter.vocChapterDOs2Page(result.getRecords(), page.getTotal());
            return pageDTO;
        }
    }

    @Override
    public void addChapter(VocChapterCommand command) {

        VocChapterDO vocChapterDO = vocChapterConverter.vocChapterCommand2DO(command);
        vocChapterMapper.insert(vocChapterDO);

    }

    @Override
    public void updateChapter(VocChapterCommand command) {
        vocChapterMapper.updateById(vocChapterConverter.vocChapterCommand2DO(command));
        vocChapterMapper.updateById(vocChapterConverter.vocChapterCommand2DO(command));

    }

    @Override
    public DeleteChapterOperateDTO deleteChapter(Long chapterId) {
        vocChapterMapper.deleteById(chapterId);
        return null;
    }

    @Override
    public List<DeleteChapterOperateDTO> deleteChapters(BatchDeleteCommand command) {
        List<DeleteChapterOperateDTO> deleteChapterOperateDTOS = vocChapterConverter.vocChapterDOs2DeleteDTOs(vocChapterMapper.selectBatchIds(command.getIds()));
        vocChapterMapper.deleteBatchIds(command.getIds());
        return null;
    }
}
